<template>
  <div>
    <el-table
      :data="userlist"
      border
      style="width: 100%"
      class="table"
    >
      <el-table-column prop="uid" label="用户编号" width="180"> </el-table-column>
      <el-table-column prop="username" label="用户名称" width="180"> </el-table-column>
      <el-table-column prop="rolename" label="所属角色" width="180"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <div>
            {{item.row.status == 1 ? '启用' : '禁用'}}
          </div>
        </template>
        <el-button type="success" plain size="mini">启用</el-button>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary" size="mini" plain @click="edit(item.row.uid)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="del(item.row.uid)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
        <!-- 分页组件 -->
    <!-- el-pagination 
    background 是否为分页按钮添加背景色
    layout 组件布局，子组件名用逗号分隔
    total	总条目数
    page-size	每页显示条目个数，支持 .sync 修饰符	number	—	10
    current-change	currentPage 改变时会触发
    -->
    <el-pagination
      :page-size="pageSize"
      background
      layout="prev, pager, next"
      :total="count"
      @current-change="changePgae"
    >
    </el-pagination>
  </div>
</template>
<script>
import {userDelete} from '../../request/api.js'
import {mapGetters} from "vuex"
export default {
  data() {
    return {
      payload: {
        size: 10, // size查询条数
        page: 1, // page页码树
      },
    };
  },
  methods: {
    // 封装一个切换页码并获取当前页码的事件
    changePgae(n){
      // console.log(n,1122123123312123312);
      this.$store.dispatch('user/changePageAction',n)
    },
    // 封装一个删除事件
    del(uid) {
      this.$confirm("你确定要删除？？？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          userDelete({ uid }).then((res) => {
            // console.log(res);
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.$store.dispatch("user/getUserListAction");
              this.$store.dispatch('user/getUserCount')
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 封装一个编辑事件
    edit(id) {
      this.$emit("edit", id);
    },
  },
  mounted() {
    // 组件一加载 触发管理员列表行动
    this.$store.dispatch("user/getUserListAction");
    this.$store.dispatch('user/getUserCount')
  },
  watch: {},
  computed: {
    ...mapGetters({
      userlist:'user/getUserList',
      pageSize:'user/getSize',
      count:'user/getCount'
    })
  },
  filters: {},
  components: {},
};
</script>
<style scoped>
.table {
  margin-top: 10px;
}
</style>
